package com.example.boardroom.demo.times;

import android.app.usage.UsageStats;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.blankj.utilcode.util.AppUtils;
import com.blankj.utilcode.util.TimeUtils;
import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.example.boardroom.R;

import java.util.List;

public class AdapterTimes extends BaseQuickAdapter<BeanTimes, BaseViewHolder> {
    public AdapterTimes(@Nullable List<BeanTimes> data) {
        super(R.layout.item_times, data);
    }

    @Override
    protected void convert(@NonNull BaseViewHolder baseViewHolder, BeanTimes bean) {
//                LogUtils.i("lufei_time", "包名:" + usageStats.getPackageName(), "开始时间: " + TimeUtils.millis2String(usageStats.getFirstTimeStamp()) + "-----  结束时间: " + TimeUtils.millis2String(usageStats.getLastTimeStamp()), "启动次数: " + "1"
//                        , "运行总时长 : " + usageStats.getTotalTimeVisible() / 1000 / 60 + "分钟", "开机时间:" + TimeUtils.millis2String(System.currentTimeMillis() - SystemClock.elapsedRealtime()), "从开机开始算，累计运行时间:" + (usageStats.getLastTimeStamp() - usageStats.getFirstTimeStamp()) / 1000 / 60 + "分钟"
//                        , "反向计算出启动时间: " + TimeUtils.millis2String((usageStats.getLastTimeStamp() - usageStats.getTotalTimeVisible())) + "---- 结束时间: " + TimeUtils.millis2String(usageStats.getLastTimeStamp()));
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {

            UsageStats usageStats = bean.usageStats;
            ImageView imageView = (ImageView) baseViewHolder.getView(R.id.iv_logo);

            baseViewHolder.setText(R.id.tv_pageName, usageStats.getPackageName());
            baseViewHolder.setText(R.id.tv_mBeginTimeStamp, "反向计算出启动时间: " + TimeUtils.millis2String((usageStats.getLastTimeStamp() - usageStats.getTotalTimeVisible())));
            baseViewHolder.setText(R.id.tv_mEndTimeStamp, "----结束时间: " + TimeUtils.millis2String(usageStats.getLastTimeStamp()));
            if (usageStats.getTotalTimeVisible() / 1000 / 60 < 1) {
                baseViewHolder.setText(R.id.tv_mTotalTimeVisible, "运行总时长 : " + usageStats.getTotalTimeVisible() / 1000 + "秒");
            } else {
                baseViewHolder.setText(R.id.tv_mTotalTimeVisible, "运行总时长 : " + usageStats.getTotalTimeVisible() / 1000 / 60 + "分钟");
            }

            try {
                Drawable appIcon = getContext().getPackageManager()
                        .getApplicationIcon(usageStats.getPackageName());
                baseViewHolder.setImageDrawable(R.id.iv_logo,appIcon);
            } catch (PackageManager.NameNotFoundException e) {
                Log.w("lufei_TAG", String.format("App Icon is not found for %s",
                        usageStats.getPackageName()));
            }

            baseViewHolder.setText(R.id.tv_ACTIVITY_RESUMED, AppUtils.isAppForeground(usageStats.getPackageName())?"可见":"不可见");
            baseViewHolder.setText(R.id.tv_isLive, AppUtils.isAppRunning(usageStats.getPackageName())?"存活":"未启动");
        }


    }
}
